package com.adxon.data;

import com.adxon.MsgException;
import com.adxon.RandomString;

import javax.servlet.http.HttpServletRequest;

public class User extends Individual{
	private class Ex extends Data{
		private String	pk;		// permalink
		private String 	pw;		// password
		private String 	sl;		// salt for password

		public Ex(){
			bsk="i11";
		}
		boolean chkPassword(String pwc){
			return pwc.equals(pw);
		}
		@Override
		protected void commitExt()throws MsgException{
			super.commitExt();
			commit("pk",pk);
			commit("pw",pw==null?"":pw);
			commit("sl",sl);
		}
		void setPassword(String pwn){
			pw=pwn;
			sl=RandomString.get(32);
		}
		@Override
		public void setup()throws MsgException{
			stp="id BIGINT NOT NULL COMMENT 'id',"+
				"pk VARCHAR(255)NOT NULL COMMENT 'account',"+
				"pw VARCHAR(255)NOT NULL COMMENT 'passwsord',"+
				"sl VARCHAR(255)NOT NULL COMMENT 'salt',"+
				"tc BIGINT NOT NULL COMMENT 'create time',"+
				"tm BIGINT NOT NULL COMMENT 'modify time',"+
				"PRIMARY KEY(pk)";
			super.setup();
		}
		@Override
		public void updateExt()throws MsgException{
			super.updateExt();
			pk=updateString("pk");
			pw=updateString("pw");
			sl=updateString("sl");
		}
	}

	public static void main(String[] args){
		try{
			User e;
			e=new User();
			e.setAccount("User");
			e.setText("User");
			e.commit();
			long i=e.getId();
			System.out.println(i);
			e=new User();
			e.update(i);
			System.out.println(e.getText());
		}catch(MsgException e){
			e.printStackTrace();
		}
	}

	private String	acc;
	private String	cfm;
	private String	pwc;			// current password
	private String	pwn;			// new password
	private Ex		uex=new Ex();	// user extra

	public void checkNewPass()throws MsgException{
		if(pwn==null || !pwn.equals(cfm))
			throw new MsgException(MsgException.NOT_MATCH,"f");
	}
	public void checkPassword()throws MsgException{
		if(pwc==null || !uex.chkPassword(pwc))
			throw acc==null?new MsgException(MsgException.PASSWORD,"p"):
				new MsgException(MsgException.ACCOUNT,"a");
	}
	@Override
	public void commit()throws MsgException{
		if(!upd)	// for new account
			this.allocateId();
		uex.id=id;
		if(acc!=null)
			uex.pk=acc;
		uex.setPassword(pwn);
		uex.commit();
		super.commit();
	}
	public String getAccount(){
		return acc;
	}
	@Override
	public void getAll(HttpServletRequest req){
		super.getAll(req);
		req.setAttribute("a",acc);
		req.setAttribute("f",cfm);
		req.setAttribute("n",pwn);
		req.setAttribute("p",pwc);
	}
	public String getPassword(){
		return pwc;
	}
	@Override
	public void reset(){
		super.reset();
		uex.reset();
	}
	public void setAccount(String account){
		acc=uex.pk=regulate(account);
	}
	public void setAll(HttpServletRequest req)throws MsgException{
		super.setAll(req);
		acc=regulate(req.getParameter("a"));
		cfm=regulate(req.getParameter("f"));
		pwc=regulate(req.getParameter("p"));
		pwn=regulate(req.getParameter("n"));
	}
	public void setNewPass(String password){
		pwn=regulate(password);
	}
	public void setPassword(String password){
		pwc=regulate(password);
	}
	@Override
	public void setTime(long time){
		super.setTime(time);
		uex.tm=time;
	}
	@Override
	public void setup()throws MsgException{
		uex.setup();
	}
	@Override
	public void updateExt()throws MsgException{
		if(id==0 && acc==null)
			throw new MsgException(MsgException.ACCOUNT,"a");
		if(id==0)
			uex.pk=acc;
		else uex.id=id;
		uex.update();
		id=uex.id;
		super.updateExt();
	}
}

